<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="考勤月份：" prop="attendanceMonth">
        <el-date-picker type="month" v-model="queryParams.attendanceMonth" format="yyyy-MM" value-format="yyyy-MM" placeholder="请选择考勤月份">
        </el-date-picker>
      </el-form-item>
      <el-form-item class="item-class" label="姓名：" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-upload2" size="mini" type="warning" @click="handleImport" v-hasPermi="['produce:attendance:import']">导入</el-button>
        <el-button icon="el-icon-download" size="mini" type="warning" @click="handleExport" v-hasPermi="['produce:attendance:download']">下载模板</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData" height="calc(100vh - 230px)">
      <el-table-column label="考勤月份" align="center" prop="attendanceMonth" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="考勤组" align="center" prop="attendanceGroup" width="120px" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="工号" align="center" prop="jobNumber" width="120" />
      <el-table-column label="职位" align="center" prop="position" width="110px" />
      <el-table-column label="出勤天数" align="center" prop="attendanceDays" />
      <el-table-column label="加班总时长" align="center" prop="overtimeHours" width="110px" />
      <el-table-column label="出勤总时长" align="center" prop="totalHours" width="110px" />
      <el-table-column label="导入用户" align="center" prop="createBy" width="130px" />
      <el-table-column label="导入时间" align="center" prop="createTime" width="120px" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <!-- 添加或修改设备维修对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form v-if="open" ref="form" :model="form" :rules="rules" label-width="auto" size="small">
        <el-form-item label="考勤月份：" prop="month">
          <el-date-picker type="month" v-model="form.month" format="yyyy-MM" value-format="yyyy-MM" placeholder="请选择考勤月份">
          </el-date-picker>
        </el-form-item>
        <el-upload class="upload-demo" ref="upload" :limit="1" accept=".xlsx, .xls" :multiple="false" action="" :http-request="uploadExcel" :file-list="fileList">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        </el-upload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAttendanceRecord,
  uploadRecord
} from "@/api/mes/pro/attendanceRecord";

export default {
  name: "AttendanceRecord",
  dicts: ["mes_workorder_status", "mes_order_type", "mes_equip_status"],
  data() {
    return {
      uploadParams: {
        url: process.env.VUE_APP_BASE_API + "/mes/pro/attendance/record/upload"
      },
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备维修表格数据
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        attendanceMonth: null,
        userName: null
      },
      // 表单参数
      form: {
        month: null,
      },
      // 表单校验
      rules: {
        month: [
          { required: true, message: '请选择考勤月份', trigger: 'change' },
        ],
      },
      fileList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleExport() {
      this.download(
        "/mes/pro/attendance/record/exportTemplate",
        {
          // ...this.queryParams,
        },
        `考勤模板.xlsx`,
        {},
        "get"
      );
    },
    uploadExcel(param) {
      console.log(param)

    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },

    handleImport() {
      this.open = true
      this.form.month = null
    },
    /** 查询设备维修列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams)
      getAttendanceRecord(this.queryParams).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
      };
      this.resetForm("form");
    },
    cancel() { },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams)
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const uploadFiles = this.$refs.upload.uploadFiles
          console.log(1111, uploadFiles)
          if (!uploadFiles.length) {
            return this.$message.error('请先上传考勤记录表！')
          }
          const that = this
          this.$modal.confirm('是否确认导入？').then(function () {
            let fd = new FormData();
            fd.append('file', uploadFiles[0].raw); // 传文件
            fd.append('month', that.form.month); // 传courseId
            uploadRecord(fd).then((resp) => {
              that.getList();
              that.$modal.msgSuccess("导入成功");
              that.open = false
            });
          }).catch((err) => {
            console.log(err)
          });

        }
      });
    },
  }
}
</script>
